package com.woniu.atms.controller.aft;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.woniu.atms.entity.G5User;
import com.woniu.atms.service.G5UserService;
import com.woniu.atms.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 李涵
 * @title: AftUserController 后台管理员 用户管理
 * @projectName g5_atms
 * @description: TODO
 * @date 2022/1/17 15:24
 */
@Slf4j
@RestController
@RequestMapping("/api/aft/v1/user")
public class AftUserController {

    @Resource
    private G5UserService g5UserService;

    /**
     * 查询用户列表
     * @return 用户列表集合
     */
//    @GetMapping("/list")
//    public ResponseResult<PageInfo<G5User>> userList(@RequestParam(required = false,defaultValue = "1")Integer pageNum){
//        PageHelper.startPage(pageNum,1);
//        List<G5User> g5Users=g5UserService.list();
//        PageInfo<G5User> pageInfo=new PageInfo<>(g5Users);
//        return new ResponseResult(pageInfo);
//    }

    /**
     * 根据id查询用户
     * @return 用户信息
     */
//    @GetMapping("/info_id")
//    public ResponseResult findUserById(@RequestBody G5User g5User){
//        System.out.println("id为"+g5User.getId());
//        return new ResponseResult<>(g5UserService.getById(g5User.getId()));
//    }

    @RequestMapping ("/userInfo")
    public ResponseResult<PageInfo> findUser(@RequestBody(required = false) G5User g5User,@RequestParam(required = false,defaultValue = "1")Integer pageNum){
        QueryWrapper queryWrapper=new QueryWrapper();
        if(g5User!=null){
            if(g5User.getId()!=null){
                queryWrapper.eq("id",g5User.getId());
            }
            if(StringUtil.isNotEmpty(g5User.getAccount())){
                queryWrapper.like("account",g5User.getAccount());
            }
        }
        PageHelper.startPage(pageNum,1);
        List<G5User> g5Users=g5UserService.list(queryWrapper);
        PageInfo<G5User> pageInfo=new PageInfo<>(g5Users);
        return new ResponseResult(pageInfo);
    }

    /**
     * 根据账号查询用户
     * @return 用户信息
     */
//    @GetMapping("/info_account")
//    public ResponseResult<G5User> findUserByAccount(@RequestBody G5User g5User){
//        QueryWrapper queryWrapper=new QueryWrapper();
//        queryWrapper.eq("account",g5User.getAccount());
//        return new ResponseResult<>(g5UserService.getOne(queryWrapper));
//    }

    /**
     * 根据id修改用户
     * @return 修改状态
     */
    @RequestMapping("/update")
    public ResponseResult updateUser(@RequestBody G5User g5User){

        return new ResponseResult(g5UserService.updateById(g5User));
    }
}
